package org.huangrui.redis.demo;


import redis.clients.jedis.Jedis;

import java.util.*;

/**
 * @Author hr
 * @Create 2024-05-01 11:48
 */
public class T01_Jedis {
    public static void main(String[] args) {
        // 1 connection获得，通过指定ip和端口号
        Jedis jedis = new Jedis("192.168.1.121", 6379);
        // 2 指定访问服务器的密码
        jedis.auth("000000");
        // 3 获得了jedis客户端，可以像jdbc一样，访问我们的redis
        System.out.println(jedis.ping());

        // keys
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);

        // string
        jedis.set("k3", "hello-jedis");
        System.out.println(jedis.get("k3"));
        System.out.println(jedis.ttl("k3"));
        jedis.expire("k3", 20L);

        // list
        jedis.lpush("list", "11", "12", "13");
        List<String> list = jedis.lrange("list", 0, -1);
        for (String element : list) {
            System.out.println(element);
        }
        // set
        System.out.println("==================SET===================");
        jedis.sadd("orders", "jd001");
        jedis.sadd("orders", "jd002");
        jedis.sadd("orders", "jd003");
        Set<String> set1 = jedis.smembers("orders");
        for (Iterator iterator = set1.iterator(); iterator.hasNext(); ) {
            String string = (String) iterator.next();
            System.out.println(string);
            jedis.srem("orders", "jd002");
            System.out.println(jedis.smembers("orders").size());
        }
        // hash
        System.out.println("==================HASH===================");
        jedis.hset("hash1", "userName", "lisi");
        System.out.println(jedis.hget("hash1", "userName"));
        Map<String, String> map = new HashMap<String, String>();
        map.put("telphone", "138xxxxxxxx");
        map.put("address", "atguigu");
        map.put("emai1", "zzyybs@126.com");// 课后有问题请给我发邮件
        jedis.hmset("hash2", map);
        List<String> result = jedis.hmget("hash2", "telphone", "address");
        for (String str:result){
            System.out.println(str);
        }
        //zset
        System.out.println("==================ZSET===================");

        jedis.zadd("zset", 50,"v1" );
        jedis.zadd("zset", 60,"v2" );
        jedis.zadd("zset", 70,"v3" );
        jedis.zadd("zset",90,"v4");

        jedis.zrange("zset",0,-1).forEach(System.out::println);
        jedis.zrandmemberWithScores("zset",2).forEach(System.out::println);

    }
}
